package crane.bomb;

import javax.swing.JTextField;

import java.awt.Dimension;
import java.awt.Font;
import java.io.Serializable;

/**
 * 
 * @author crane
 *
 */
public class TimerText extends JTextField implements Runnable, Serializable{
    
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6888503741727456822L;

    /**
     * Constructor
     *
     */
    public TimerText(){
        this.setText(0);
        this.setPreferredSize(new Dimension(45, 35));
        this.setHorizontalAlignment(0);
        this.setEnabled(false);
        this.setFocusable(false);
        this.setFont(new Font("Arial", Font.BOLD, 24));
        this.setDisabledTextColor(BombColor.C_TEXT_READONLY_FORE);
        this.setBackground(BombColor.C_TEXT_READONLY_BACK);
    }
    
    /**
     * run
     */
    public void run() {
        keepRunning();
    }
    
    /**
     * is TimeOut
     * @return boolean
     */
    protected boolean isTimeOut() {
        return timeouted;
    }
    
    
    protected void startTimer(){
        if (isRunning()){
            return;
        }
        startRunning();
        Thread t = new Thread(this);
        t.start();
    }
    
    protected void stopTimer(){
        if (!isRunning()){
            return;
        }
        this.stop();
    }
    
    /**
     * Stop timer
     *
     */
    synchronized void stop() {
        running = false;
    }
    
    /*
     * running
     */
    private boolean running = false;
    
    /*
     * timeouted
     */
    private boolean timeouted = false;

    /*
     * time
     */
    private int time = 0;
    
    /*
     * ZERO_STRING
     */
    private static String ZERO_STRING = "000";

    /**
     * keepRunning
     */
    private void keepRunning(){
        while (running){
            try{
                time = this.getValue();
                if (time > 998){
                    timeouted = true;
                    running = false;
                    return;
                }
                Thread.sleep(1000);
                time++;
                this.setText(time);
            }catch (Exception e){
                this.setText(0);
                running = false;
                return;
            }
        }
    }

    /**
     * is Running
     * @return boolean
     */
    private boolean isRunning() {
        return running;
    }
    
    /**
     * start Running
     */
    private void startRunning() {
        this.running = true;
    }


    
    /**
     * Set Text
     * @param text
     * @return int
     */
    private int getValue(){
        int iText = 0;
        try{
            String sText = super.getText(0, 3);
            iText = Integer.parseInt(sText);
        }catch (Exception e){
            // do nothing
        }
        return iText;
    }
    
    /**
     * Set Text
     * @param text
     */
    private void setText(int text){
        String tempStr = ZERO_STRING + text;
        this.setText(tempStr.substring(tempStr.length() - 3));
    }


}
